﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Lucene.Net.Search;
using Lucene.Net.Documents;

namespace LuceneWInFormDemo
{
    public partial class Form1 : Form
    {
        private Indexer indexer;
        private Searcher searcher;
        private string indexPath;
        private TopDocCollector result;
        private string query;

        public Form1()
        {
            InitializeComponent();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void FindIndexFolder_Click(object sender, EventArgs e)
        {
            DialogResult res = indexFolderBrowserDialog.ShowDialog();
            indexTextBox.Text = indexFolderBrowserDialog.SelectedPath;
            indexPath = indexTextBox.Text;
        }

        private void FindDataFolder_Click(object sender, EventArgs e)
        {
            DialogResult res = dataFolderBrowserDialog.ShowDialog();
            dataTextBox.Text = dataFolderBrowserDialog.SelectedPath;
        }

        private void indexButton_Click(object sender, EventArgs e)
        {
            indexer = new Indexer(indexPath);
            int ant = indexer.Index(dataTextBox.Text, checkBoxDelete.Checked);
            long time = indexer.TimeLastAction;

            IndexStatus.AppendText(Environment.NewLine);
            IndexStatus.AppendText("Number of documents indexed      : " + ant + Environment.NewLine);
            IndexStatus.AppendText("Runtime to index documents (msec): " + time + Environment.NewLine);
            IndexStatus.AppendText("Time of finishing indexing       : " + DateTime.Now.ToUniversalTime() + Environment.NewLine);
        }

        private void searchButton_Click(object sender, EventArgs e)
        {
            query = searchTextBox.Text;
            searcher = new Searcher(indexPath);
            result = searcher.Search(query);
            ScoreDoc[] hits = result.TopDocs().scoreDocs;
            foreach (ScoreDoc sd in hits)
            {
                int docId = sd.doc;
                Document d = searcher.Doc(docId);
                SearchResult.AppendText("" + docId + ". " + d.Get("title"));
                //Console.WriteLine((i + 1) + ". " + d.Get("title"));
            }
        }
    }
}
